import { ref, onMounted } from 'vue';
import { useRoute,onBeforeRouteUpdate } from 'vue-router';
import { getTopCategoryAPI } from '@/apis/category';

export function useCategory() {
    // 路由
    const route = useRoute();
    // 获取数据
    const categoryData = ref({});
    const getTopCategory = async (id) => {
        const res = await getTopCategoryAPI(id);
        categoryData.value = res.result;
    }

    onMounted(() => {
        getTopCategory(route.params.id);
    })


    // 路由钩子，解决路由缓存页面不更新
    onBeforeRouteUpdate((to) => {
        console.log(to.params.id);
        getTopCategory(to.params.id);
    })

    return {
        categoryData
    }
}